class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]



  def home

  end

  # GET /users
  # GET /users.json
  def index
    @users = User.all
    #@users = User.find_by_sql("select * form rpt_ordings and users where ")
    @date=Time.now.strftime("%Y%m%d")
    @today=@date.to_time.strftime('%Y-%m-%d %H:%M:%S')
    @lastday=(@date+"235959").to_time.strftime('%Y-%m-%d %H:%M:%S')
    @ordings = RptOrding.where("created_at between ? and ? and ording = ? ", @today, @lastday, 1)
    #return @ording_users={users:@users,ordings:@ordings}
    #获取所有用户
    @orings=User.find_by_sql("select *,u.id as uid,o.id as oid from (select * from users) u left outer join (select * from rpt_ordings where created_at between '#{@today}' and '#{@lastday}' and ording = 1
    ) o on o.user_id=u.id  ")

    @count=0
    @orings.each do |ording|
      if ording.ording==1
        @count+=1
      end
    end
    #遍历每个user查询是否订餐
=begin
    @ordingUsers={}
    @users.each do  |user|
      @ordingUsers.
    end
=end
  end

  def ording
    @users = User.all
    #@users = User.find_by_sql("select * form rpt_ordings and users where ")
    @date=Time.now.strftime("%Y%m%d")
    @today=@date.to_time.strftime('%Y-%m-%d %H:%M:%S')
    @lastday=(@date+"235959").to_time.strftime('%Y-%m-%d %H:%M:%S')
    @ordings = RptOrding.where("created_at between ? and ? and ording = ? ", @today, @lastday, 1)
    #return @ording_users={users:@users,ordings:@ordings}
    #获取所有用户
    @orings=User.find_by_sql("select *,u.id as uid,o.id as oid from (select * from users) u left outer join (select * from rpt_ordings where created_at between '#{@today}' and '#{@lastday}' and ording = 1
    ) o on o.user_id=u.id  ")

    @count=0
    @orings.each do |ording|
      if ording.ording==1
        @count+=1
      end
    end
  end

  def unording
    @user = User.find(params[:uid])
    @ording=RptOrding.find(params[:oid])
    @ording.destroy
    #RptOrding.find_by_sql("delete from rpt_ordings where id =#{:oid}")
    redirect_to(:action => 'ording')
  end

  def do_ording
    #@user = User.find(params[:uid])
    #@o=RptOrding.new(ording: 1,user_id: :uid)
    RptOrding.create(ording: 1, user_id: params[:uid])
    redirect_to(:action => 'ording')
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    #@@ording=RptOrding.find_by(:user_id,@user.id)
    puts @user.id

    #@ording.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_user
    @user = User.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def user_params
    params.require(:user).permit(:name)
  end
end
